import { asyncRouterMap, constantRouterMap } from '@/router'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(routes, authority, supperAdmin) { // eslint-disable-line
  if (supperAdmin) { // 超级管理员获得添加所有路由
    return routes
  }
  // if (!authority.length) {
  //   return []
  // }
  let authObj = authority.reduce((total,item) => {
    total[item.menuUrl] = item
    return total
  },{})
  const res = []
  let dic = {
    userinfo: 'user',
    auth: 'userAuthority',
    systemconfig: 'systemConfig',
    carmileageconfig: 'vehicleMileageWarnConfig',
    cartimeconfig: 'vehicleTimeWarnConfig',
    personconfig: 'empWarnConfig',
  }
  let dicArr = ['user','userAuthority','systemConfig','vehicleTimeWarnConfig','vehicleTimeWarnConfig','empWarnConfig']
  routes.forEach(route => {
    const tmp = { ...route }
    if (dic.hasOwnProperty(route.name)) {
      if (authObj.hasOwnProperty(dic[route.name])) {
        if (authObj[dic[route.name]].authorityAll) {
          res.push(tmp)
        }
      }
    } else {
      if (tmp.children) {
        tmp.children = filterAsyncRouter(tmp.children, authority)
        if (tmp.children.length != 0) {
          tmp.redirect = '/'+tmp.name+'/'+tmp.children[0].name // 将父级路由的重定向修改为第一个子路由
          res.push(tmp)
        }
      } else {
        res.push(tmp)
      }
    }
  })

  return res
}

const permission = {
  state: {
    routers: constantRouterMap,
    hasSetRoute: false,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.hasSetRoute = true
      state.routers = constantRouterMap.concat(routers)
    },
    SET_HASSETROUTER_FALSE(state){
      state.hasSetRoute = false
    },
  },
  actions: {
    GenerateRoutes({ state, rootState, commit }, data) {
      console.log('GenerateRoutes')
      return new Promise(resolve => {
        let authority = rootState.user.authority
        let addRouters = filterAsyncRouter(asyncRouterMap,authority,rootState.user.supperAdmin)
        console.log(addRouters)


        // let addRouters = asyncRouterMap.filter(item => {
        //   if (item.menuUrl in dicArr) {
        //     if (item.menuUrl == 'user') {

        //     }
        //   }
        //   if (authObj.hasOwnProperty('systemConfig') && authObj.systemConfig.authorityAll) {
        //     return true
        //   }
        // })
        // const { roles } = data
        // let accessedRouters
        // // 'boss'
        // if (roles.includes('admin')) {
        //   accessedRouters = [asyncRouterMap[0], asyncRouterMap[2]]
        // } else if (roles.includes('boss')) {
        //   accessedRouters = [asyncRouterMap[1], asyncRouterMap[2]]
        // } else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        commit('SET_ROUTERS', addRouters)
        resolve()
      })
    }
  }
}

export default permission
